package LearnConcurrent;

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @description:
 * @author: ywk
 * @date: 2020-08-12
 */
public class BlockqueueTest {
    static  BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(5);
    static AtomicInteger count = new AtomicInteger(10);
    public static  class Consumer implements Runnable {

        @Override
        public void run() {
            while (true) {
                Integer take = null;
                try {
                    take = queue.take();
                } catch (InterruptedException e) {
                    System.out.println(Thread.currentThread().getName());
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+":::"+100 / take);

            }
        }
    }
    public static  class Producer implements Runnable {

        @Override
        public void run() {
            while (count.decrementAndGet() > -10) {
                try {
                    queue.put(count.intValue());

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        new Thread(new Producer()).start();
        new Thread(new Consumer()).start();
        new Thread(new Consumer()).start();
    }

}
